package com.example.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TokenService {
    private static final long EXPIRE_TIME = 5 * 60 * 1000; // 设置过期时间

    public String getToken(User user) {
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        String token = JWT.create()
                .withAudience(user.getId().toString()) // 将 user id 保存到 token 里面
                .withExpiresAt(date) // 设置过期时间
                .sign(Algorithm.HMAC256(user.getPassword())); // 使用用户密码作为密钥
        return token;
    }
}
